function LinkedList() {

  this.head = null // head
  this.length = 0 // 记录链表的长度
  this.current = null // 记录最后一个节点

  // 内部结点类
  function Node(data) {
    this.data = data
    this.next = null // 指向下一个结点的引用
  }

  // 1.append方法,向链表末尾追加结点
  LinkedList.prototype.append = function(data) {
    let newNode = new Node(data)
    if (this.length === 0) {
      this.head = newNode
      this.current = newNode
    } else {
      this.current.next = newNode
      this.current = newNode
    }
    this.length++
  }

  // 2.向链表指定位置插入节点
  LinkedList.prototype.insert = function(position, data) {
    // 1.如果该位置不存在
    if (position > this.length || position < 0) return false

    // 2.该位置存在
    let newNode = new Node(data)
    let crt = this.head // 当前结点
    let index = 1
    while (crt) {
      if (position === 0) {
        newNode.next = crt
        this.head = newNode
        break
      } else if (position === index++) {
        newNode.next = crt.next
        crt.next = newNode
        break
      }
      crt = crt.next
    }
    this.length++
  }

  // 3.获取对应位置的元素
  LinkedList.prototype.get = function(position) {
    // 判断越界
    if (position > this.length || position <= 0) return null
    let index = 1
    let crt = this.head // 记录当前结点
    while (crt) {
      if (index++ === position) {
        return crt.data
      }
      crt = crt.next
    }
  }

  // 4.返回元素在链表中的索引，如果没用就返回-1
  LinkedList.prototype.indexOf = function(data) {
    let index = 1
    let crt = this.head // 记录当前结点
    while (crt) {
      if (crt.data === data) {
        return index
      }
      index++
      crt = crt.next
    }
    return -1
  }

  // 5.修改某个位置的元素
  LinkedList.prototype.update = function(position, data) {
    // 先判断是否越界
    if (position > this.length || position <= 0) return false

    let index = 1
    let crt = this.head // 当前结点
    while (crt) {
      if (index++ === position) {
        crt.data = data
        break
      }
      crt = crt.next
    }
  }

  // 6.从列表的特定位置移除一项
  LinkedList.prototype.removeAt = function(position) {
    // 判断位置
    if (position > this.length || position <= 0) return false

    let index = 1
    let crt = this.head // 记录当前结点
    while (crt) {
      if (++index === position) {
        let temp = crt.next.data
        crt.next = crt.next.next
        return temp
      }
      crt = crt.next
    }
  }

  // 7.从链表中移除一项
  LinkedList.prototype.remove = function(data) {
    // let index = 1
    // let crt = this.head // 记录当前指向的结点
    // while (crt) {
    //   if (index++ === this.length - 1) {
    //     let temp = crt.next.data
    //     crt.next = null
    //     return temp
    //   }
    //   crt = crt.next
    // }
    let index = this.indexOf(data)
    return this.removeAt(index)
  }

  // 8.判断链表是否为空
  LinkedList.prototype.isEmpty = function() {
    return this.length === 0
  }

  // 9.返回链表元素的个数
  LinkedList.prototype.size = function() {
    return this.length
  }

  // 10.输出链表
  LinkedList.prototype.toString = function(punctuation) {
    let crt = this.head // 表示当前指向的节点
    let strArr = []
    while (crt) {
      strArr.push(crt.data)
      crt = crt.next
    }
    return strArr.join(punctuation)
  }
}


// module.exports = LinkedList
let l = new LinkedList()
l.append(1)
l.append(2)
console.log(l.toString(""));
// l.append('cuifan')
// l.append('cuifan1')
// l.append('cuifan2')
// l.append('cuifan3')
// l.insert(2, 'simon')
// console.log(l.toString());
// console.log(l.get(l.length));
// console.log(l.indexOf('cuifan3'));
// console.log(l.removeAt(5));
// console.log(l.remove());
// console.log(l.toString());
// console.log(l.isEmpty());
// console.log(l.size());
// console.log(l.remove('simon'));
// console.log(l.toString());